package com.jsg.sl651.util;

import com.alibaba.fastjson2.JSONArray;
import com.jsg.sl651.entity.city.CityDefinition;
import com.jsg.sl651.entity.city.CityInfo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class ChineseCity {

    @lombok.Data
    public static class Data {
        private String code;
        private String value;
        private List<Data> children;
    }
    private static List<Data> DATA_LIST;
    static {
        InputStream resourceAsStream = ChineseCity.class.getResourceAsStream("/txt/CyUnit.txt");
        if (resourceAsStream != null) {
            String collect = new BufferedReader(new InputStreamReader(resourceAsStream)).lines().parallel().collect(Collectors.joining(System.lineSeparator()));
            DATA_LIST = JSONArray.parseArray(collect, Data.class);
        }
    }

    public static List<Data> getCityLists() {
        return DATA_LIST;
    }

    public static Data findByCode(String code) {
        return findByCode(code,null);
    }

    public static Data findByCode(String code,List<Data> listData) {
        if (listData == null) {
            listData = DATA_LIST;
        }
        Data result =null;
        if (listData == null) {
            return result;
        }
        for (Data data : listData) {
            if (data.getCode().equals(code)) {
                result = data;
            }
            if (result == null && data.getChildren() != null && !data.getChildren().isEmpty()) {
                result = findByCode(code,data.getChildren());
            }
            if (result != null) {
                break;
            }
        }
        return result;
    }

    public final static Map<String, CityInfo> CITY_DEFINITION_MAP = new HashMap<>();
    public final static Map<String, CityDefinition> CITY_BEAN_MAP = new HashMap<>();
    static {
        List<Data> cityLists = getCityLists();
        for (Data cityList : cityLists) {
            add(cityList);
        }
    }


    public static void add(Data data) {
        CITY_DEFINITION_MAP.put(data.getCode(), new CityInfo(data.getCode(), data.getValue()));
        if (data.getChildren() != null && !data.getChildren().isEmpty()) {
            for (Data data1 : data.getChildren()) {
                add(data1);
            }
        }
    }

    public static CityDefinition getCityDefinition(String code) {
        if(code == null || code.length() < 2 || code.length() % 2 != 0 || code.length() > 6) {
            throw new IllegalArgumentException("code is null or code length is not even");
        }
        StringBuilder codeBuilder = new StringBuilder(code);
        while(codeBuilder.length() < 6) {
            codeBuilder.append("00");
        }
        code = codeBuilder.toString();
        CityDefinition cityDefinition = CITY_BEAN_MAP.get(code);
        if(cityDefinition == null) {
            synchronized (CITY_BEAN_MAP) {
                cityDefinition = CITY_BEAN_MAP.get(code);
                if(cityDefinition == null) {
                    cityDefinition = new CityDefinition(
                            CITY_DEFINITION_MAP.get(code.substring(0, 2) + "0000"),
                            CITY_DEFINITION_MAP.get(code.substring(0, 4) + "00"),
                            CITY_DEFINITION_MAP.get(code.substring(0, 6))
                    );
                    CITY_BEAN_MAP.put(code, cityDefinition);
                }
            }
        }
        return cityDefinition;
    }


//    public static void main(String[] args) {
//        CityDefinition cityDefinition = getCityDefinition("622927");
//        System.out.println(cityDefinition);
//    }

}
